package com.slg.android.manyanime.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.PlayActivity;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.adapter.EpisodeAdapter;
import com.slg.android.manyanime.bean.Episode;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class EpisodeFragment extends Fragment implements OnClickCallBack {
    @ViewInject(R.id.episode_fragment_recyclerView)
    private RecyclerView recyclerView;
    @ViewInject(R.id.episode_fragment_error_info_textView)
    private TextView errorInfo;
    private ArrayList<Episode> data;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        Serializable episode = arguments.getSerializable("episode");
        data = (ArrayList<Episode>) episode;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_episode, container, false);
        ViewUtils.inject(this, view);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 5);
        recyclerView.setLayoutManager(layoutManager);
        if (data != null && data.size() > 0) {
            if(errorInfo.getVisibility() == View.VISIBLE){
                errorInfo.setVisibility(View.GONE);
            }
            EpisodeAdapter adapter = new EpisodeAdapter(getActivity(), data);
            adapter.setListener(this);
            recyclerView.setAdapter(adapter);
        }else{
            errorInfo.setVisibility(View.VISIBLE);
        }

        return view;
    }


    @Override
    public void onItemClick(View v, int position) {
        Episode episode = data.get(position);
        Intent intent = new Intent(getActivity(), PlayActivity.class);
        intent.putExtra("url",episode.getmUrl());
        startActivity(intent);
    }
}
